package com.cory.admin.sys.core.privilege.tag;

import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.cory.admin.sys.config.AdminConstants;
import com.cory.admin.sys.domain.AdminPrincipal;
import com.cory.boot.config.FreemarkerConfig.FreemarketTag;
import com.cory.boot.domain.enumdef.EDataFlag;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * @author xiongcong
 * @date 2022-05-02
 */
@Component
@FreemarketTag(name = "areSuperUser", prefix="privilege")
public class AreSuperUserTag implements TemplateDirectiveModel {
    
    @Autowired
    private HttpServletRequest request;
    
    @Override
    @SuppressWarnings({"rawtypes"})
    public void execute(Environment env, Map params, TemplateModel[] templateModels, TemplateDirectiveBody body) throws TemplateException, IOException {
        
        boolean areSuperUser = false;
        AdminPrincipal principal = (AdminPrincipal)request.getSession().getAttribute(AdminConstants.SESSION_USER_INFO);
        if (principal != null && principal.getSuperFlag() != null && principal.getSuperFlag() == EDataFlag.YES.key) {
            areSuperUser = true;
        } 
        
        if (areSuperUser && body != null)
            body.render(env.getOut());
    }
}
